import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.security.spec.RSAOtherPrimeInfo;
import java.util.Scanner;

public class TcpEchoClient {
    private Socket clientSocket ;

    public TcpEchoClient(String serverIp, int serverPort) throws IOException {
       this.clientSocket = new Socket(serverIp , serverPort);
    }
    public void start(){
        System.out.println("客户端启动");
        Scanner scan = new Scanner(System.in);
        try(InputStream inputStream = clientSocket.getInputStream();
            OutputStream outputStream = clientSocket.getOutputStream()){
            Scanner scanResponse = new Scanner(inputStream) ;
            while (true){
                System.out.println("输入请求->");
                String request = scan.next() ;
                PrintWriter writer = new PrintWriter(outputStream) ;
                writer.println(request);
                writer.flush();
                String response = scanResponse.next();
                System.out.println(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client = new TcpEchoClient("127.0.0.1" , 9090);
        client.start();
    }
}
